﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using SZHL.Data;
using SZHL.UI.AppCode;
using FastReflectionLib;
using Newtonsoft.Json;
using System.Data;

namespace SZHL.UI.API
{
    public class GUANZHUManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(GUANZHUManage).GetMethod(msg.Action.ToUpper());
            GUANZHUManage model = new GUANZHUManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 添加关注信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDGUANZHU(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            JH_Auth_GUANZHU GUANZHU = JsonConvert.DeserializeObject<JH_Auth_GUANZHU>(P1);

            if (GUANZHU.ID == 0)
            {
                GUANZHU.CRDate = DateTime.Now;
                GUANZHU.CRUser = UserInfo.User.UserName;
                new JH_Auth_GUANZHUB().Insert(GUANZHU);
            }
            else
            {
                new JH_Auth_GUANZHUB().Update(GUANZHU);
            }
            msg.Result = GUANZHU;
        }
        /// <summary>
        /// 添加关注用户
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDGUANZHULIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            JH_Auth_GUANZHU GUANZHU = new JH_Auth_GUANZHU(); 
            GUANZHU.CRDate = DateTime.Now;
            GUANZHU.CRUser = UserInfo.User.UserName;
            GUANZHU.GZUser = UserInfo.User.UserName;
            if (P1=="")
            {
                msg.ErrorMsg = "请选择关注人";
                return;
            }
            string [] GuanZhuUser = P1.Split(',');
            foreach (string str in GuanZhuUser)
            {
                 if(new JH_Auth_GUANZHUB().GetEntities(d=>d.BGZUser==str&&d.GZUser==UserInfo.User.UserName).Count()==0)
                 { 
                    GUANZHU.BGZUser = str;
                    new JH_Auth_GUANZHUB().Insert(GUANZHU);
                 }
            }
            
            msg.Result = GUANZHU;
        }
        /// <summary>
        /// 获取关注信息列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETGUANZHULIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = string.Format(" select * from JH_Auth_GUANZHU where GZUser='{0}'", UserInfo.User.UserName);
            DataTable dt = new JH_Auth_GUANZHUB().GetDTByCommand(strSql);
            msg.Result = dt;
        }
        /// <summary>
        /// 删除关注信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">ID</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELGUANZHUBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                if (!new JH_Auth_GUANZHUB().Delete(d => d.ID.ToString() == P1))
                {

                    msg.ErrorMsg = "删除失败";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 获取关注信息信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETGUANZHUMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new JH_Auth_GUANZHUB().GetEntity(d => d.ID.ToString() == P1);
        }
    }
}